#encoding=utf8
import pandas as pd
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import cross_validation, metrics
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import matplotlib.pylab as plt


if __name__ =="__main__":
    train = pd.read_csv('../dataset/train_modified.csv')
    target='Disbursed' # Disbursed的值就是二元分类的输出
    IDcol = 'ID'
    print train['Disbursed'].value_counts()

    x_columns = [x for x in train.columns if x not in [target, IDcol]]
    X = train[x_columns]
    y = train['Disbursed']

    #划分训练集、测试集
    x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=1)


    gbdt_0 = GradientBoostingClassifier(random_state=10)
    gbdt_0.fit(x_train, y_train)
    y_pred = gbdt_0.predict(x_test)
    y_predprob = gbdt_0.predict_proba(x_test)[:, 1]
    print "Accuracy (Test): %.4g" % metrics.accuracy_score(y_test.values, y_pred)
    print "AUC Score (Test): %f" % metrics.roc_auc_score(y_test, y_predprob)

    print ""
    #GridSearchCV 调参数param_test1
    param_test1 = {'n_estimators': range(20, 81, 10)}
    gbdt_gscv_1 = GridSearchCV(estimator=GradientBoostingClassifier(learning_rate=0.1, min_samples_split=300,
                                                                 min_samples_leaf=20, max_depth=8, max_features='sqrt',
                                                                 subsample=0.8, random_state=10),
                            param_grid=param_test1, scoring='roc_auc', iid=False, cv=5)
    gbdt_gscv_1.fit(x_train, y_train)
    print gbdt_gscv_1.cv_results_
    print gbdt_gscv_1.best_params_,gbdt_gscv_1.best_score_

    y_pred = gbdt_gscv_1.predict(x_test)
    y_predprob = gbdt_gscv_1.predict_proba(x_test)[:, 1]
    print "Accuracy (Test): %.4g" % metrics.accuracy_score(y_test.values, y_pred)
    print "AUC Score (Test): %f" % metrics.roc_auc_score(y_test, y_predprob)


    print ""
    # GridSearchCV 调参数param_test2
    param_test2 = {'max_depth': range(3, 14, 2), 'min_samples_split': range(100, 801, 200)}
    gbdt_gscv_2 = GridSearchCV(
        estimator=GradientBoostingClassifier(learning_rate=0.1, n_estimators=60, min_samples_leaf=20,
                                             max_features='sqrt', subsample=0.8, random_state=10),
        param_grid=param_test2, scoring='roc_auc', iid=False, cv=5)
    gbdt_gscv_2.fit(x_train, y_train)
    print gbdt_gscv_2.cv_results_
    print gbdt_gscv_2.best_params_,gbdt_gscv_2.best_score_

    y_pred = gbdt_gscv_2.predict(x_test)
    y_predprob = gbdt_gscv_2.predict_proba(x_test)[:, 1]
    print "Accuracy (Test): %.4g" % metrics.accuracy_score(y_test.values, y_pred)
    print "AUC Score (Test): %f" % metrics.roc_auc_score(y_test, y_predprob)
